//
//  LMHomePageC.m
//  liman_UrineAnalyer
//
//  Created by lanou3g on 16/4/28.
//  Copyright © 2016年 liman. All rights reserved.
//

#import "LMHomePageC.h"
#import "CK_MenuViewController.h"
#import "LMCycleView.h"
#import "UIImage+ImageEffects.h"
#import "JSONKit.h"
#import "LMHomeModel.h"
#import "LMHomePageCell.h"

#define Kwidth [UIScreen mainScreen].bounds.size.width
#define Kheight [UIScreen mainScreen].bounds.size.height
@interface LMHomePageC ()

@property (nonatomic, strong) LMCycleView *cycleView;

@property (nonatomic, strong) NSMutableArray *dataArray;
@end

@implementation LMHomePageC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, 200)];
    [self setImageArray: [@[@"1.jpg",@"2.jpg",@"3.jpg",@"4.jpg",@"5.jpg"] mutableCopy]];
    UIImage *image =[UIImage imageNamed:@"beijing1.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[image applyDarkEffect]];
       [self reloadData];
    [self.tableView registerNib:[UINib nibWithNibName:@"LMHomePageCell" bundle:nil] forCellReuseIdentifier:@"LMHomePageCell"];
}
#pragma mark - 首页轮播图
- (void)setImageArray:(NSMutableArray *)mArrray{
    self.cycleView = [[LMCycleView alloc]initWithFrame:CGRectMake(0, 0, Kwidth, 200) interVal:3];
    self.cycleView.imgNameArray = mArrray;
    [self.tableView.tableHeaderView addSubview:self.cycleView];
    
    
}
#pragma mark - 弹出左侧视图
- (IBAction)didClickSetupLeftController:(UIBarButtonItem *)sender {
    
    if ([CK_MenuViewController getMenuViewController].isShowing) {
        [[CK_MenuViewController getMenuViewController] hideLeftViewController];
    }else{
        [[CK_MenuViewController getMenuViewController] showLeftViewController];
    }
    
}

#pragma mark - 解析数据

- (void)reloadData{
    NSString *file = [[NSBundle mainBundle]pathForResource:@"MovieList" ofType:@"txt"];
    NSData *data = [NSData dataWithContentsOfFile:file];
    
    NSMutableDictionary *dataDict  = [[JSONDecoder decoder]mutableObjectWithData:data];
    NSArray *movieArray = dataDict[@"result"];
    NSLog(@"%@",data);
    NSLog(@"%@",dataDict);
    self.dataArray = [NSMutableArray array];
    
    for (NSDictionary *movieItem in movieArray) {
        LMHomeModel *model = [[LMHomeModel alloc]init];
        [model setValuesForKeysWithDictionary:movieItem];
        [self.dataArray addObject:model];
    }
    for (LMHomeModel *model in self.dataArray) {
        NSLog(@"%@",model.title);
    }


}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LMHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LMHomePageCell" forIndexPath:indexPath];
    LMHomeModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
